package com.audaque.springboot.foshanupload.web.nosaveframeworkloginvalid.config;


import com.audaque.springboot.foshanupload.authcore.properties.LoginProperties;
import com.audaque.springboot.foshanupload.core.constants.ComConst;
import com.audaque.springboot.foshanupload.core.properties.StaticResourcePathProperties;

import com.audaque.springboot.foshanupload.web.nosaveframeworkloginvalid.interceptor.NeedLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author zgb
 * @desc Interceptor用于鉴证和鉴权
 * @date 2022-01-25 22:24:15
 */
@Configuration
public class LoginValidInterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private StaticResourcePathProperties staticResourcePathProperties;

    @Autowired
    private NeedLoginInterceptor needLoginInterceptor;

    @Autowired
    private LoginProperties loginProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {


        //表示要拦截
        String[] addPathPatterns = {
                ComConst.INTERCEPTOR_ANY_MATCH
        };


        //要排除的路径
        List<String> excludePathPatterns= staticResourcePathProperties.getList();
        List<String> unAuthenticatedUrlList = loginProperties.getUnAuthenticatedUrlList();
        unAuthenticatedUrlList.addAll(excludePathPatterns);
        String excludedUris = String.join(",", unAuthenticatedUrlList);


        //用注解识别是否需要登录，而不是路径
        registry.addInterceptor(needLoginInterceptor).addPathPatterns(addPathPatterns).excludePathPatterns(excludedUris).order(100);


    }


}
